package com.fly.jface.plugin.action;
//OpenAction.java

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.operation.ModalContext;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;

import com.fly.jface.plugin.FileManager;
import com.fly.jface.plugin.PluginWindow;

public class OpenAction extends Action
{
    public OpenAction()
    {
        super();
        setText("打开(&O)");
        setToolTipText("打开文件");
    }
    
    @Override
    public void run()
    {
        FileDialog dialog = new FileDialog(PluginWindow.getApp().getShell(), SWT.OPEN);
        dialog.setFilterExtensions(new String[] {"*.zip", "*.*"});
        final String name = dialog.open();
        if ((name == null) || (name.length() == 0))
        {
            return;
        }
        final FileManager fileManager = PluginWindow.getApp().getManager();
        try
        {
            ModalContext.run(new IRunnableWithProgress()
            {
                @Override
                public void run(IProgressMonitor progressMonitor)
                {
                    progressMonitor.beginTask("打开文件", IProgressMonitor.UNKNOWN);
                    fileManager.load(name);
                    progressMonitor.done();
                }
            }, true, PluginWindow.getApp().getStatusLineManager().getProgressMonitor(), PluginWindow.getApp().getShell().getDisplay());
        }
        catch (InvocationTargetException e)
        {
            e.printStackTrace();
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
        PluginWindow.getApp().getContent().setText(fileManager.getContent());
        PluginWindow.getApp().getStatusLineManager().setMessage("当前打开的文件是: " + name);
    }
}